#define  _CRT_SECURE_NO_WARNINGS
#include"seqlist.h"
void SLinit(SL* s1)
{
	s1->a = (type*)malloc(sizeof(type) * 4);
	if (s1->a == NULL)
	{
		perror("malloc fail");
		return ;
	}
	s1->capacity = 4;
	s1->size = 0;

}
void SLD(SL* s1)
{
	free(s1->a);
	s1->a = NULL;
	s1->size = 0;
	s1->capacity = 0;
}
void SLCheckCapacity(SL* s1)
{
	if (s1->size == s1->capacity)
	{
		type* tmp = (type*)realloc(s1->a, sizeof(type) * s1->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		s1->a = tmp;
		s1->capacity *= 2;
	}
}
void SLPushFront(SL* s1, type x)
{
	SLCheckCapacity(s1);
	int end = s1->size - 1;
	while(end>=0)
	{
		s1->a[end + 1] = s1->a[end];
		end--;
	}
	s1->a[0] = x;
	s1->size++;
}
void SLPopBack(SL* s1)
{
	assert(s1->size > 0);
	/*if (s1->size == 0)
	{
		return;
	}*/
	s1->size--;
	if (s1->size == 0)
	{
		s1->size = 0;
	}
}
void SLPrint(SL* s1)
{
	for (int i = 0; i < s1->size; i++)
	{
		printf("%d ", s1->a[i]);
	}
	printf("\n");
}
void SLPushBack(SL* s1, type x)
{
	SLCheckCapacity(s1);
	s1->a[s1->size] = x;
	s1->size++;
}
void SLPopFront(SL* s1)
{
	assert(s1->size > 0);
	int start = 0;
	while (start<s1->size-1)
	{
		s1->a[start] = s1->a[start + 1];
		start++;
	}
}
void SLInsert(SL* s1, int pos, type x)
{
	assert(0 <= pos&& pos>=s1->size);
	SLCheckCapacity(s1);
	int end = s1->size;
	while (pos<end)
	{
		s1->a[end] = s1->a[end - 1];
		end--;
	}
	s1->a[pos] = x;
	s1->size++;
}
void SLErase(SL* s1, int pos)
{
	assert(0 <= pos && pos < s1->size);
	//assert(0 < s1->size);
	while (pos<s1->size-1)
	{
		s1->a[pos] = s1->a[pos + 1];
		pos ++ ;
	}
	s1->size--;

}
int SLFind(SL* s1, type x)
{
	int i = 0;
	for (i = 0; i < s1->size; i++ )
	{
		if (s1->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void SLModify(SL* s1, int pos, type x)
{
	assert(0 <= pos && pos < s1->size);
	s1->a[pos] = x;
}